﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Youz.Common.Helper;
using Youz.CreditRepay.BLL;
using Youz.CreditRepay.Entity;

namespace Youz.CreditRepay.Server.PayEase
{
    public partial class PayCallback : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Pay_PaymentType paymentType = new Pay_PaymentTypeBLL().Get("PayEase");
                var payRecordsBLL = new Pay_RecordsBLL();

                string v_oid_ = Request["v_oid"].ToString();//订单编号
                string v_pmode_ = Request["v_pmode"].ToString();//支付方式
                string v_pstatus_ = Request["v_pstatus"].ToString();//支付结果 20 支付成功，30支付失败
                string v_pstring_ = Request["v_pstring"].ToString();//支付结果信息
                string v_md5info_ = Request["v_md5info"].ToString();//数字指纹
                string v_amount_ = Request["v_amount"].ToString();//订单金额
                string v_moneytype_ = Request["v_moneytype"].ToString();//币种
                string v_md5money_ = Request["v_md5money"].ToString();//数字指纹
                string v_sign_ = Request["v_sign"].ToString();//RSA的数字指纹

                ClassHmac2003.ClassHmac cc = new ClassHmac2003.ClassHmac();
                string mm = v_oid_ + v_pstatus_ + HttpUtility.UrlEncode(v_pstring_).ToUpper() + HttpUtility.UrlEncode(v_pmode_).ToUpper(); ;
                string md5info = cc.hex_hmac_md5(mm, paymentType.MerchantKey);//test密钥

                string md = v_amount_ + v_moneytype_;
                string md5money = cc.hex_hmac_md5(md, paymentType.MerchantKey);

                string tradeNo = v_oid_.Split('-')[2];
                var results = new Pay_PayEaseResults();
                //写支付结果表
                results.PaymentResultsID = Guid.NewGuid();
                results.v_oid = v_oid_;
                results.v_pmode = v_pmode_;
                results.v_pstatus = v_pstatus_;
                results.v_pstring = v_pstring_;
                results.v_md5info = v_md5info_;
                results.v_amount = v_amount_;
                results.v_moneytype = v_moneytype_;
                results.v_md5money = v_md5money_;
                results.v_sign = v_sign_;
                results.md5info = md5info;
                results.md5money = md5money;
                results.CreateTime = DateTime.Now;
                results.Memo = tradeNo;
                results.Type = 0;
                new Pay_PayEaseResultsBLL().Add(results);

                if (md5info == v_md5info_ && md5money == v_md5money_)
                {
                    var payRecords = payRecordsBLL.Get(tradeNo);

                    if (payRecords != null)
                    {
                        //订单支付已经成功
                        if (v_pstatus_ == "20")
                        {
                            if (payRecords.Status != PayRecordsStatus.Payed && payRecordsBLL.UpdateStatus(payRecords.TradeNo, PayRecordsStatus.Payed))
                            {
                                //充值
                                var rechargeBLL = new Member_RechargeBLL();
                                var amount = TypeHelper.ConvertTo<decimal>(v_amount_);
                                var recharge = rechargeBLL.Get(payRecords.ParentID);
                                rechargeBLL.SaveAudit(RechargeStatus.Audited, amount, "在线支付", recharge);
                            }

                            Page.Response.Redirect("PayFinish.html?t=1", false);
                            return;
                        }
                        else if (v_pstatus_ == "30")
                        {
                            payRecordsBLL.UpdateStatus(payRecords.TradeNo, PayRecordsStatus.Close);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Core.WriteErrorLog(ex);
            }

            Page.Response.Redirect("PayFinish.html?t=0", false);
        }

    }
}